﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Utility;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Exam.Bussiness;
using Portal.Exam.Model;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class AddExamRoom
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AddExamRoom));
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string AccountType = WebEnvironments.Request(SiteId, "AccountType");
                if (string.IsNullOrEmpty(AccountType)) AccountType = "";

                string ExamSegmentId = (string)WebEnvironments.Request(SiteId, "ExamSegmentId");
                ExamSegmentCls OExamSegment = new ExamSegmentBll().CreateModel(SiteId, ExamSegmentId);
                if (OExamSegment == null) throw new Exception("Kỳ thi không đúng. Tham số không hợp lệ!");
                string BackUrl = "ExamRoom.aspx";
                string SelectActiveRoom =
                    "<select id=drpSelectActive>\r\n" +
                    "   <option value=\"0\">Ngưng sử dụng</option>\r\n" +
                    "   <option value=\"1\">Sử dụng</option>\r\n" +
                    "</select>\r\n";
                StringBuilder RetHtml = new StringBuilder();
                RetHtml.Append(
                    "<input type=hidden id=txtAccountType value=\"" + AccountType + "\">\r\n" +
                    "<script language=javascript>\r\n" +
                    " function ClientSaveAddExamRoom()\r\n" +
                    "     {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         AccountType=document.getElementById(\"txtAccountType\").value;\r\n" +
                    "         ExamSegmentId=document.getElementById('txtExamSegmentId').value;\r\n" +
                    "         RoomNo=document.getElementById(\"txtRoomNo\").value;\r\n" +
                    "         ExamSubject=document.getElementById(\"txtExamSubject\").value;\r\n" +
                    "         BuildingInfo=document.getElementById(\"txtBuildingInfo\").value;\r\n" +
                    "         Teacher1=document.getElementById(\"txtTeacher1\").value;\r\n" +
                    "         Teacher2=document.getElementById(\"txtTeacher2\").value;\r\n" +
                    "         StartTime=document.getElementById(\"txtStartTime\").value;\r\n" +
                    "         Duration=document.getElementById(\"txtDuration\").value;\r\n" +
                    "         ActiveRoom=document.getElementById(\"drpSelectActive\").value;\r\n" +
                    "         ExamTitle=document.getElementById(\"txtExamTitle\").value;\r\n" +
                    "         ExamDescription=document.getElementById(\"txtExamDescription\").value;\r\n" +
                    "         AjaxOut = Portal.Exam.Render.AddExamRoom.ServerSideAddExamRoom(SiteId,AccountType, ExamSegmentId, RoomNo, ExamSubject, BuildingInfo, Teacher1, Teacher2, StartTime, Duration, ActiveRoom, ExamTitle, ExamDescription).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "             alert(AjaxOut.InfoMessage);\r\n" +
                    "             return;\r\n" +
                    "         }\r\n" +
                    "         window.open('" + BackUrl + "','_self');\r\n" +
                    "     }\r\n" +

                    "</script>\r\n" +
                " <div class=DatabaseFunction>\r\n" +
                "        <ul>\r\n" +
                "            <li onclick=\"javascript:ClientSaveAddExamRoom();\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                "            <li onclick=\"javascript:window.open('" + BackUrl + "','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                "        </ul>\r\n" +
                "  </div>\r\n" +
                "   <input id=\"txtExamSegmentId\" type=hidden value=\"" + ExamSegmentId + "\">\r\n" +
                "   <h3>Thêm mới phòng thi</h3>\r\n" +
                "   <div class=CssFormSaveUpdate>\r\n" +
                "     <table style=\"width:99%\">" +
                "          <tr>" +
                "              <td class=\"Col1\">Kỳ thi:</td>" +
                "              <td>" + OExamSegment.ExamSegmentName + "</td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Phòng thi:</td>" +
                "              <td><input class=textbox id=txtRoomNo></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Môn thi:</td>" +
                "              <td><input class=textbox id=txtExamSubject></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Thông tin phòng thi:</td>" +
                "              <td><input class=textbox id=txtBuildingInfo></td>" +
                "          </tr>" +

                "          <tr>" +
                "              <td class=\"Col1\">Tiêu đề bài thi:</td>" +
                "              <td><input class=textbox id=txtExamTitle></td>" +
                "          </tr>" +

                "          <tr>" +
                "              <td class=\"Col1\">Hướng dẫn làm bài:</td>" +
                "              <td><textarea class=textarea id=txtExamDescription></textarea></td>" +
                "          </tr>" +

                "          <tr>" +
                "              <td class=\"Col1\">Giám khảo 1:</td>" +
                "              <td><input class=textbox id=txtTeacher1></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Giám khảo 2:</td>" +
                "              <td><input class=textbox id=txtTeacher2></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Ngày thi:</td>" +
                "              <td>" + LookUpUtilities.GenerateLookUpDate("txtStartTime", System.DateTime.Now) + "</td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Thời gian thi</td>" +
                "              <td><input id=txtDuration type=textbox class=textboxshort value=\"60\"> phút</td>" +
                "          </tr>" +

                "          <tr>" +
                "              <td class=\"Col1\">Trạng thái:</td>" +
                "              <td>" + SelectActiveRoom + "</td>" +
                "          </tr>" +
                "      </table>" +
                "</div>\r\n");

                
                OAjaxOut.HtmlContent = RetHtml.ToString();
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideAddExamRoom(
            string SiteId,
            string AccountType,
            string ExamSegmentId,
            string RoomNo,
            string ExamSubject,
            string BuildingInfo,
            string Teacher1,
            string Teacher2,
            string StartTime,
            int Duration,
            int ActiveRoom,
            string ExamTitle,
            string ExamDescription)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                if (string.IsNullOrEmpty(RoomNo))
                {
                    throw new Exception("Chưa nhập thông tin phòng thi!");
                }
                if (string.IsNullOrEmpty(ExamSubject))
                {
                    throw new Exception("Chưa nhập thông tin môn thi!");
                }
                if (!FunctionUtilities.checkVnDate(StartTime))
                {
                    throw new Exception("Ngày thi nhập không đúng!");
                }

                DateTime dStartTime = FunctionUtilities.VNDateToDate(StartTime);
                new ExamRoomBll().CreateExamRoom(
                    null,
                    SiteId,
                    AccountType,
                    RoomNo,
                    ExamSubject,
                    BuildingInfo,
                    Teacher1,
                    Teacher2,
                    dStartTime,
                    Duration,
                    ActiveRoom,
                    ExamSegmentId,
                    ExamTitle,
                    ExamDescription,
                    WebSessionUtility.GetCurrentLoginUser().AssetCode);
                OAjaxOut.InfoMessage = "Thêm mới phòng thi thành công!";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}